#!/usr/bin/env python
#
#       ModBrowse.py
#
#       Copyright 2008 karthikeyan S.<kaarthikeyapreyan@gmail.com>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import os
import wx
import sys
import inspect

import accessories
import displaydefinitions

class Selectmodule:
    def __init__(self,baseframe):
        """
        BaseFrame Instantiation
        """
        self.Inst=baseframe

    def openmod(self,event):
        """
        Selects and writes the module to the TextEntry
        """
        self.Inst.modname.Clear()
        filedlg = wx.FileDialog(self.Inst, "Browse Module",\
            os.getcwd(), "", "*.*", wx.OPEN)
        if filedlg.ShowModal() == wx.ID_OK:
            path = str(filedlg.GetPath())
            selected = os.path.basename(path).split(".")[0]
            self.Inst.modname.WriteText(selected)
            sys.path.append(path.rstrip(os.path.basename(path)))
        filedlg.Destroy()

class Modhelp:
    def __init__(self,baseframe):
        self.Inst=baseframe

    def gatherinfo(self,event):
        """
        Gather selected info
        """
        entity=displaydefinitions.Displayselected(self.Inst).retrive()
        module=str(entity[0])
        item=str(entity[1])
        if len(entity) >= 4 :
            mclass=str(entity[3])
        else:
            mclass=None

        module=__import__(module)

        if not mclass :
            myobj=eval("module.%s"%item)
        else :
            myobj=eval("module.%s.%s"%(mclass,item))

        sourcefile=inspect.getfile(myobj)
        docstring=inspect.getdoc(myobj)
        comment=inspect.getcomments(myobj)

        self.disp([sourcefile,docstring,comment])

    def disp(self,pdat):
        """
        Publish Source file, docstring and comment
        """
        accessories.infobox\
            (self.Inst,"\t\tSouceFile : %s\n\n\
            DocString : %s\n\n\
            Source Comments : %s\n\n"\
            %(pdat[0],pdat[1],pdat[2]),"Developer Docs",wx.OK)

